package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.POJO.Orders;
import com.example.reggie.service.OrdersService;
import com.example.reggie.utility.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;


    //用户下单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    //订单查询
    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(int page, int pageSize){
        Page<Orders> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(page1, lambdaQueryWrapper);
        //需要商品详细的DTO然后对照着填入数据即可
//        Page<OrderDetail> page2 = new Page<>();
//        BeanUtils.copyProperties(page1, page2, "records");
//        List<Orders> records = page1.getRecords();
//        List<OrderDetail> list = records.stream().map((item) -> {
//            OrderDetail orderDetail = new OrderDetail();
//            //处理逻辑
//            return orderDetail;
//        }).collect(Collectors.toList());
//        page2.setRecords(list);
//        return R.success(page2);
        return R.success(page1);
    }}
